/*
 *  Copyright (C) 2010 MADSA.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author:  Jorge Pintado de Santiago
 *          
 */

package com.madsa.AugmentedReality;

import java.util.ArrayList;

import com.sisof.R;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.view.View;

public	class DrawOnTop extends View implements SensorEventListener{
    private ArrayList<Double> destines;
    private Drawable item;
    
	public DrawOnTop(Context context, ArrayList<Double> destines) {
		super(context);
		this.destines = destines;
		Drawable item = context.getResources().getDrawable(R.drawable.cine);
		this.item=item;
		
        updateOrientation(new float[] {0, 0, 0});
	}
		

	protected void onDraw(Canvas canvas) {
		
		Paint paint = new Paint();
		paint.setStyle(Paint.Style.FILL);
		paint.setColor(Color.BLACK);
		paint.setTextSize(30);
		canvas.drawText(""+ bearing, 100, 100, paint);
		
		for(Double destine:destines){
			Paint p = new Paint();
			p.setStyle(Paint.Style.FILL);
			p.setColor(Color.BLUE);
			if (Math.abs(destine.floatValue()-bearing)<30){ //no saldra en la pantalla, asi que por cuestion de eficiencia no dibujamos
				float position = (destine.floatValue()-bearing)*this.getWidth()/60 + this.getWidth()/2; 
//				canvas.drawCircle(position, this.getHeight()/2, 50, p);
				item.setBounds((int)position, this.getHeight()/2-item.getIntrinsicHeight()/2, (int)position+item.getIntrinsicWidth(), this.getHeight()/2+item.getIntrinsicHeight()/2);
				item.draw(canvas);
			}
		}
		
		invalidate();
	} 
	
	private float bearing=0;
	private float bearingAnt=180;
	float pitch = 0;
	float roll = 0;
	
	public void setBearing(float _bearing) {
		if (Math.abs(_bearing+90-bearingAnt)>4 && !(Math.abs(_bearing)<5)){
			bearing = _bearing+90;
			bearingAnt = bearing;
			if (bearing<0){
				bearing+=360;
			}
		}
	}
	
	

	public float getBearing() {
		return bearing;
	}

	public float getPitch() {
		return pitch;
	}
	public void setPitch(float pitch) {
		this.pitch = pitch;
	}
	public float getRoll() {
		return roll;
	}
	public void setRoll(float roll) {
		this.roll = roll;
	}

	public void onAccuracyChanged(Sensor sensor, int accuracy) {
	}

//	private float   mOrientationValues[] = new float[3];
	private float   mAccelerometerValues[] = new float[3];
	private float   mMagneticValues[] = new float[3];
//	private float   mTemperatureValues;
	
	public void onSensorChanged(SensorEvent event) {
			
		// Cada sensor puede provocar que un thread pase por aquí, así 
		// que sincronizamos el acceso
		synchronized (this) {
			switch(event.sensor.getType()) {
//			case Sensor.TYPE_ORIENTATION:
//				for (int i=0 ; i<3 ; i++) {
//					mOrientationValues[i] = event.values[i];
//				}
//				break;

			case Sensor.TYPE_ACCELEROMETER:
				for (int i=0 ; i<3 ; i++) {
					mAccelerometerValues[i] = event.values[i];
				}
				break;
			case Sensor.TYPE_MAGNETIC_FIELD:
				for (int i=0 ; i<3 ; i++) {
					mMagneticValues[i] = event.values[i];
				}
				break;

//			default:
//				for (int i=0 ; i<event.values.length ; i++) {
//					mTemperatureValues = event.values[i];
//				}
			}
			updateOrientation(mMagneticValues);
		}

	}
	
    private void updateOrientation(float[] values) {
    	setBearing(values[0]);
    	setPitch(values[1]);
    	setRoll(-values[2]);
    	invalidate();
    }
    
//    private float[] calculateOrientation() {
//    	float[] values = new float[3];
//    	float[] R = new float[9];
//  	  	SensorManager.getRotationMatrix(R, null, mAccelerometerValues, mMagneticValues);
//  	  	SensorManager.getOrientation(R, values);
//  	  	// Convert from Radians to Degrees.
//  	  	values[0] = (float) Math.toDegrees(values[0]);
//  	  	values[1] = (float) Math.toDegrees(values[1]);
//  	  	values[2] = (float) Math.toDegrees(values[2]);
//  	  	return values;
//    }
}